/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package DataAccess;

import BussinesLogic.Request;
import DataModel.*;
import java.sql.*;

public class Dao_Colaborador {
    
    public Dao_Colaborador(){}

    public Request consultarColaborador(String campo, int tipoConsulta) {
        /* 
         * Si tipo de consulta es 1 la busqueda del usuario se hace por identificacion
         * Si es 2 la busqueda se realiza por username
         * Campo es el valor para realizar la busqueda                
         */
        Colaborador colaborador = new Colaborador();
        EventoDeportivo evento = new EventoDeportivo();
        Request request = new Request();
        String sql = "";

        switch (tipoConsulta) {
            case 1:
                sql = "SELECT * FROM \"Colaborador\" WHERE \"identificacion\"='" + campo + "'";
                break;
            case 2:
                sql = "SELECT * FROM \"Colaborador\" WHERE \"username\"='" + campo + "'";
                break;
        }
        try {
            Connect conn = new Connect();
            driverPGSQL driver = new driverPGSQL(conn);
            request = driver.querySelect(sql);
            ResultSet rqs = request.getRequest();
            while (rqs.next()) {
                colaborador.setIdentificacion(rqs.getString(1));
                colaborador.setUsername(rqs.getString(2));
                colaborador.setPassword(rqs.getString(3));
                colaborador.setCargo(rqs.getString(4));
                evento.setId(rqs.getString(5));
            }
            colaborador.setEvento(evento);
            request.setObject(colaborador);
            conn.close();
        } catch (Exception e) {
            request.setTransac(false);
            request.setMessage(e.getMessage());
        }

        return request;
    }
    
    public Request registrarColaborador(Colaborador colaborador){
        
        String sql= "INSERT INTO \"Colaborador\" (identificacion, cargo, username, password, \"idEvento\")"
                + " VALUES('" + colaborador.getIdentificacion() + "','" + colaborador.getCargo()
                + "','" + colaborador.getUsername() + "','" + colaborador.getPassword() + "','" 
                + colaborador.getEvento().getId() + "')";
        Request request = new Request();

        try {
            Connect conn = new Connect();
            driverPGSQL driver = new driverPGSQL(conn);
            request = driver.queryInsert(sql);
            conn.close();
        } catch (Exception e) {
            request.setTransac(false);
            request.setMessage(e.getMessage());
        }
        return request;        
    }
    
    public Request modificarColaborador(String id, String campo, String valor){
        
        String sql = "UPDATE \"Colaborador\" SET \"" + campo + "\"='" + valor +"' WHERE identificacion='" + id + "'";
        Request request = new Request();
        
        try {
            Connect conn = new Connect();
            driverPGSQL driver = new driverPGSQL(conn);
            request = driver.queryUpdate(sql);
            conn.close();
        } catch (Exception e) {
            request.setTransac(false);
            request.setMessage(e.getMessage());
        }
        return request;  
    }
    
}
